<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="报销日期" prop="expenseTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.expenseTime"
          type="month"
          value-format="yyyy-MM-dd"
          placeholder="选择报销日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="报销人" prop="expenseName">
        <el-input
          v-model="queryParams.expenseName"
          placeholder="请输入报销人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="报销金额" prop="totalAmountMin">
        <el-input style="width:25%"
          v-model="queryParams.totalAmountMin"
          placeholder=""
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />-
        <el-input style="width:25%"
          v-model="queryParams.totalAmountMax"
          placeholder=""
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['product:cashAudit:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="cashAuditList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      

      <el-table-column prop="expenseTime" label="报销日期">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.cashTimeStart,'{y}-{m}-{d}') + '至' + parseTime(scope.row.cashTimeEnd,'{y}-{m}-{d}')}}</span>
        </template>
      </el-table-column>


      <el-table-column label="报销人姓名" align="center" prop="expenseName" />
      
      <el-table-column label="报销总金额" align="center" prop="totalAmount" />
      
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['product:cashAudit:audit']"
          >审核</el-button>

          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleViewProcess(scope.row)"
          >审核进度</el-button>-->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改现金报销单审核对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%">
      <cashShow :propCashId="cashId" ></cashShow>

      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        
        <el-form-item label="审核结果" style="margin-top:15px">
          <el-select v-model="form.auditOption" placeholder="请选择">
              <el-option
                  v-for="dict in auditOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
              ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="审核意见" >
          <el-input v-model="form.comment" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看流程进度图 -->
    <el-dialog :title="title" :visible.sync="open1" width="800px">
      <el-form ref="form1" :model="form1" label-width="800px">
          <img :src="form1.img" style="width:700px;height:400px" class="img-circle img-lg" />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCashAudit, cashAuditHandle,   exportCashAudit ,getProcessImg} from "@/api/product/cashAudit";
import cashShow from "@/views/product/cash/view/cashShow";

export default {
  name: "CashAudit",
  components:{
        cashShow
    },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 现金报销单审核表格数据
      cashAuditList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层(流程图)
      open1: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        expenseTime: undefined,
        expenseName: undefined,
        totalAmountMin: undefined,
        totalAmountMax: undefined
      },
      // 表单参数
      form: {},
      form1: {},
      cashId:"",
      // 审核字典
      auditOptions: [],
      // 表单校验
      rules: {
        jobGroup: [
          { required: true, message: "审核结果不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_audit").then(response => {
      this.auditOptions = response.data;
    });
  },
  methods: {
    /** 查询现金报销单审核列表 */
    getList() {
      this.loading = true;
      listCashAudit(this.queryParams).then(response => {
        this.cashAuditList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        expenseTime: undefined,
        expenseName: undefined,
        cashTimeStart: undefined,
        cashTimeEnd: undefined,
        productId: undefined,
        totalAmount: undefined,
        createTime: undefined,
        createBy: undefined,
        updateTime: undefined,
        updateBy: undefined,
        delFlag: undefined,
        instanceStartTime: undefined,
        instanceEndTime: undefined,
        instanceId: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 审核按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      const taskId = row.taskId
      this.form.id = id ;
      this.cashId = id ;
      this.form.taskId = taskId ;
      this.open = true;
    },
    /** 点击审核保存 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            if(this.form.auditOption == 0){
              if(this.form.comment == undefined){
                this.msgError("请填写审核意见！")
                return ;
              }
            }
            cashAuditHandle(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("审核成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } 
        }
      });
    },
    /** 查看审核进度 */
    handleViewProcess(row) {
      const instanceId = row.instanceId
      this.form1.instanceId = instanceId ;
      this.form1.img = '';
      getProcessImg(this.form1).then(res => {
        this.form1.img = "data:image/gif;base64," + res.img;
        this.open1 = true;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有现金报销单审核数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCashAudit(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>